const TOOL = {
  getSignalData (data, device, signal) {
    let val = {};
    data.forEach(v => {
      if (v.name == device) {
        (v.signalList || []).forEach(item => {
          if (item.name == signal) {
            val = item;
          }
        });
      }
    });
    return val;
  },
  objArraySort (objArr, key, type = 'des') {
    let result = objArr.slice(0);
    // 倒序
    if (type === 'des') {
      return result.sort((a, b) => b[key] - a[key]);
    }
    // 正序
    if (type === 'asc') {
      return result.sort((a, b) => a[key] - b[key]);
    }
  },
  // 格式化日期 前台传值方式  引用类.dateFormat(1402233166999,"yyyy-MM-dd hh:mm:ss")
  dateFormat: (date, fmt) => {
    let getDate = new Date(date);
    let o = {
      'M+': getDate.getMonth() + 1,
      'd+': getDate.getDate(),
      'h+': getDate.getHours(),
      'm+': getDate.getMinutes(),
      's+': getDate.getSeconds(),
      'q+': Math.floor((getDate.getMonth() + 3) / 3),
      'S': getDate.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (let k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
      }
    }
    return fmt;
  },
  // 设置和修改cookie
  setCookie: (name, value, iDay) => {
    /* iDay 表示过期时间 name是设置cookie名称
    cookie中 = 号表示添加，不是赋值 */
    let oDate = new Date();
    oDate.setDate(oDate.getDate() + iDay);
    document.cookie = name + '=' + value + ';expires=' + oDate;
  },
  // 获取cookie
  getCookie: (name) => {
    /* 获取浏览器所有cookie将其拆分成数组 */
    let arr = document.cookie.split('; ');
    for (let i = 0; i < arr.length; i++) {
      /* 将cookie名称和值拆分进行判断 */
      let arr2 = arr[i].split('=');
      if (arr2[0] === name) {
        return arr2[1];
      }
    }
    return '';
  },
  // 清空所有cookie;
  clearCookie: () => {
    let cookie = document.cookie;
    let cookieArray = cookie.split('; '); // 将所有键值对分开
    let cookieKey = []; // 保存cookie中键值名的数组
    for (let i = 0; i < cookieArray.length; i++) {
      let keyValue = cookieArray[i].split('=');
      cookieKey[cookieKey.length] = keyValue[0]; // 将对应的cookie的键存入数组中
    }
    for (let i = 0; i < cookieKey.length; i++) {
      this.setCookie(cookieKey[i], 1, -1); // 只要将cookie的保存日期设置为-1就可以清除kookie
    }
    console.log('已经成功清除所有cookie信息！');
  },
  // 获取当前月份的第一天
  showMonthFirstDay: () => {
    let Nowdate = new Date();
    let Moth, Day;
    let MonthFirstDay = new Date(Nowdate.getYear(), Nowdate.getMonth(), 1);
    Moth = Number(MonthFirstDay.getMonth()) + 1;
    Moth = Moth.toString().padStart(2, 0);
    Day = MonthFirstDay.getDate().toString().padStart(2, 0);
    return Nowdate.getFullYear() + '-' + Moth + '-' + Day;
  },
  // 当前月的最后一天
  showMonthLastDay: () => {
    let Nowdate = new Date();
    let Moth, Day;
    let MonthNextFirstDay = new Date(Nowdate.getYear(), Nowdate.getMonth() + 1, 1);
    let MonthLastDay = new Date(MonthNextFirstDay - 86400000);
    Moth = Number(MonthLastDay.getMonth()) + 1;
    Moth = Moth.toString().padStart(2, 0);
    Day = MonthLastDay.getDate().toString().padStart(2, 0);
    return Nowdate.getFullYear() + '-' + Moth + '-' + Day;
  },
  // 判断是否为空
  isEmpty: (obj) => {
    if (typeof obj === 'undefined' || obj === null || obj === '') {
      return true;
    } else {
      return false;
    }
  },
  // 深拷贝
  deepClone: (obj) => {
    let objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === 'object') {
        for (let key in obj) {
          if (obj.hasOwnProperty(key)) {
            // 判断ojb子元素是否为对象，如果是，递归复制
            if (obj[key] && typeof obj[key] === 'object') {
                objClone[key] = TOOL.deepClone(obj[key]);
            } else {
                // 如果不是，简单复制
                objClone[key] = obj[key];
            }
          }
        }
    }
    return objClone;
  },
  // 解析URL参数
  getQueryString: (name) => {
    var reg = new RegExp('(\\?|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.href.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
  },
    // 获取用户菜单id
  getMenuId: (url) => {
    let rId;
    let userMenu = window.localStorage.getItem('user-menu');
    if (userMenu) {
      userMenu = JSON.parse(userMenu);
      userMenu.forEach((item) => {
        if (item.children) {
          item.children.forEach((em) => {
            if (em.url === url) {
              rId = em.id;
            }
          });
        }
      });
      return rId;
    }
  },
  // 获取上月年月信息
  getPreMonth: () => {
    let currDate = new Date();
    let currYear = currDate.getFullYear();
    let currMonth = currDate.getMonth() + 1;
    let preYear = currYear;
    let preMonth = parseInt(currMonth) - 1;
    if (preMonth == 0) {
        preYear = parseInt(preYear) - 1;
        preMonth = 12;
    }
    if (preMonth < 10) {
        preMonth = '0' + preMonth;
    }
    let preYearMonth = preYear + '-' + preMonth;
    return preYearMonth;
  },
  // 清除树懒加载返回的数组数据中的children，不然会影响element-ui
  dealTreeDataForElementUI (data) {
    if (Array.isArray(data)) {
      for (let ele of data) {
        if (Array.isArray(ele.children)) {
          ele.children = [];
        }
      }
    }
    return data;
  }
};
export default TOOL;
